package core.util.logging.screen;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ScreenCapture
{
    private static final String IMG_FORMAT = "png";

    private static final String WORKING_DIR = System.getProperty( "user.dir" );

    public static void takeScreenShot( String fileName ) {
        fileName = fileName + "." + IMG_FORMAT;

        // fileName = WORKING_DIR + File.pathSeparator + "img"
        // + File.pathSeparator + fileName + "." + IMG_FORMAT;
        try {
            Robot robot = new Robot();
            Rectangle captureSize = new Rectangle( Toolkit.getDefaultToolkit()
                    .getScreenSize() );
            BufferedImage bufferedImage = robot
                    .createScreenCapture( captureSize );

            robot.keyPress( KeyEvent.VK_ALT );
            robot.keyPress( KeyEvent.VK_PRINTSCREEN );
            robot.keyRelease( KeyEvent.VK_PRINTSCREEN );
            robot.keyRelease( KeyEvent.VK_ALT );
            ImageIO.write( bufferedImage, IMG_FORMAT, new File( fileName ) );
        } catch( AWTException e ) {
            System.err.println( "Errors in taking screenshot" );
            e.printStackTrace();
        } catch( IOException e ) {
            System.err.println( "Errors in saving image to: " + fileName );
            e.printStackTrace();
        }
    }

    public static String getFileDirectory() {
        return WORKING_DIR;
    }
}
